package com.mm.mweather.service

import android.app.AlarmManager
import android.app.PendingIntent
import android.app.Service
import android.content.Context
import android.content.Intent
import android.content.SharedPreferences
import android.os.IBinder
import android.os.SystemClock
import android.preference.PreferenceManager
import android.widget.Toast

import com.mm.mweather.gson.Weather
import com.mm.mweather.util.HttpUtil
import com.mm.mweather.util.Utility
import okhttp3.Call
import okhttp3.Callback
import okhttp3.Response
import java.io.IOException

/**
 * Created by hmm on 19-2-26.
 */

class AutoUpdateService : Service() {
    override fun onBind(intent: Intent): IBinder? {
        return null
    }

    override fun onStartCommand(intent: Intent, flags: Int, startId: Int): Int {
        updateWeather()
        updateBingPic()
        var alarmManager: AlarmManager = getSystemService(ALARM_SERVICE) as AlarmManager
        var anHour:Int = 8*60*60*100
        var  triggerAtTime:Long = SystemClock.elapsedRealtime()+anHour
        var i:Intent = Intent(this@AutoUpdateService,AutoUpdateService::class.java)
        var pi:PendingIntent = PendingIntent.getService(this@AutoUpdateService,0,i,0)
        alarmManager.cancel(pi)
        alarmManager.set(AlarmManager.ELAPSED_REALTIME_WAKEUP,triggerAtTime,pi)
        return super.onStartCommand(intent, flags, startId)
    }

    private fun updateWeather() {
        val prefs = PreferenceManager.getDefaultSharedPreferences(this)
        val weatherString = prefs.getString("weather", null)
        if (weatherString != null) {
            var weather:Weather = Utility.handleWeatherResponse(weatherString)
            var weatherId:String? = weather.basic!!.weatherId
            var address:String="https://free-api.heweather.net/s6/weather?location="+weatherId+"&key=f9aeb541625441499a2b8d89aba9e08c"
            HttpUtil.sendOkHttpRequests(address, object : Callback {
                override fun onFailure(call: Call, e: IOException) {

                   e.printStackTrace()

                }


                override fun onResponse(call: Call, response: Response) {
                    val resposeText = response.body()!!.string()
                    val weather:Weather=Utility.handleWeatherResponse(resposeText)
                        if (weather!=null&&"ok".equals(weather.status)){
                            val editor:SharedPreferences.Editor=PreferenceManager.getDefaultSharedPreferences(this@AutoUpdateService).edit()
                            editor.putString("weather",resposeText)
                            editor.apply()
                        }
                }
            })
        }
    }

    private fun updateBingPic(){
        val picUrl:String = "http://guolin.tech/api/bing_pic";
        HttpUtil.sendOkHttpRequests(picUrl,object : Callback{

            override fun onFailure(call: Call?, e: IOException?) {
                e!!.printStackTrace()
            }


            override fun onResponse(call: Call?, response: Response) {
                val bingPic:String=response.body()!!.string()

                var editor:SharedPreferences.Editor=PreferenceManager.getDefaultSharedPreferences(this@AutoUpdateService).edit()
                editor.putString("bing_pic",bingPic)
                editor.apply()
            }
        })
    }
}
